package com.example.system_backend.controller;

import com.example.system_backend.entity.Teeth;
import com.example.system_backend.entity.User;
import com.example.system_backend.repository.UserRepository;
import com.example.system_backend.service.TeethService;
import com.example.system_backend.service.UserService;
import com.example.system_backend.util.ResultVOUtil;
import com.example.system_backend.vo.DtTeethVO;
import com.example.system_backend.vo.ResultVO;
import com.example.system_backend.vo.TeethResultVO;
import com.example.system_backend.vo.TeethVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/teeth")
public class TeethHandler {

    @Autowired
    private TeethService teethService;
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/teethListByUser")
    public ResultVO teethListByUser(@RequestParam(value = "email") String userEmail) {
        List<TeethVO> voList = new ArrayList<>();
        List<Teeth> teethList = teethService.teethListByUser(userEmail);
        for (Teeth teeth: teethList) {
            TeethVO teethVO = new TeethVO();
            teethVO.setId(teeth.getId());
            teethVO.setName(teeth.getName());
            teethVO.setTime(teeth.getTime());
            int type = teeth.getType();
            if (type == 1) {
                teethVO.setType("上牙");
            } else {
                teethVO.setType("下牙");
            }
            voList.add(teethVO);
        }
        return ResultVOUtil.success(voList);
    }

    @GetMapping("/teethById")
    public ResultVO teethById(@RequestParam(value = "id") Integer id) {
        TeethResultVO vo = new TeethResultVO();
        Teeth teeth = teethService.teethById(id);
        List<Integer> results = new ArrayList<>();
        results.add(teeth.getL1());
        results.add(teeth.getL2());
        results.add(teeth.getL3());
        results.add(teeth.getL4());
        results.add(teeth.getL5());
        results.add(teeth.getL6());
        results.add(teeth.getL7());
        results.add(teeth.getR1());
        results.add(teeth.getR2());
        results.add(teeth.getR3());
        results.add(teeth.getR4());
        results.add(teeth.getR5());
        results.add(teeth.getR6());
        results.add(teeth.getR7());
        vo.setResults(results);
        int type = teeth.getType();
        if (type == 1) {
            vo.setType("upper");
        } else {
            vo.setType("lower");
        }
        return ResultVOUtil.success(vo);
    }

    @GetMapping("/teethListByDentist")
    public ResultVO teethListByDentist(@RequestParam(value = "email") String dtEmail) {
        List<DtTeethVO> voList = new ArrayList<>();
        List<Teeth> teethList = teethService.teethListByDentist(dtEmail);
        for (Teeth teeth: teethList) {
            DtTeethVO dtTeethVO = new DtTeethVO();
            dtTeethVO.setId(teeth.getId());
            dtTeethVO.setName(teeth.getName());
            dtTeethVO.setTime(teeth.getTime());
            int type = teeth.getType();
            if (type == 1) {
                dtTeethVO.setType("上牙");
            } else {
                dtTeethVO.setType("下牙");
            }
            dtTeethVO.setUser_email(teeth.getUserEmail());
            User user = userRepository.findByEmail(teeth.getUserEmail());
            dtTeethVO.setUser_name(user.getName());
            voList.add(dtTeethVO);
        }
        return ResultVOUtil.success(voList);
    }
}
